package io.renren.modules.diycake.service.impl;

import io.renren.common.exception.RRException;
import io.renren.modules.diycake.dao.UserAccountDao;
import io.renren.modules.diycake.entity.UserAccountEntity;
import io.renren.modules.diycake.service.UserAccountService;
import io.renren.modules.diycake.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.diycake.dao.DiyUserDao;
import io.renren.modules.diycake.entity.DiyUserEntity;
import io.renren.modules.diycake.service.UserService;
import org.springframework.util.StringUtils;


@Service("userMainService")
public class UserServiceImpl extends ServiceImpl<DiyUserDao, DiyUserEntity> implements UserService {
  @Autowired
  private UserAccountDao userAccountMapper;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<DiyUserEntity> page = this.page(
            new Query<DiyUserEntity>().getPage(params),
            new QueryWrapper<DiyUserEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public Integer login(UserAccountEntity userAccount) {
    String phone = userAccount.getPhone();
    String password = userAccount.getPassword();

    if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
      throw new RRException("请输入用户名或密码");
    }

    QueryWrapper<UserAccountEntity> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("phone", phone);
    Integer count = userAccountMapper.selectCount(queryWrapper);

    if (count == 0) {
      throw new RRException( "未查到该用户");
    }

    UserAccountEntity account = userAccountMapper.selectOne(queryWrapper);

    if (!account.getPassword().equals(MD5.encrypt(password))) {
      throw new RRException("密码错误");
    }
    UserAccountEntity userAccount1 = userAccountMapper.selectOne(queryWrapper);
    return userAccount1.getUserId();
  }

  @Override
  public boolean register(UserAccountEntity userAccount) {
    String phone = userAccount.getPhone();
    String password = userAccount.getPassword();

    if (phone.isEmpty() || password.isEmpty()) {
      throw new RRException("有电话或者密码为空!");
    }

    QueryWrapper<UserAccountEntity> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("phone", phone);
    Integer count = userAccountMapper.selectCount(queryWrapper);
    if (count > 0) {
      throw new RRException("该电话号码已经被注册");
    }

    Integer allCount = userAccountMapper.selectCount(null);
    userAccount.setUserId(++allCount);
    userAccount.setPassword(MD5.encrypt(password));
    userAccount.setId(allCount);

    DiyUserEntity user = new DiyUserEntity();
    user.setId(allCount);
    baseMapper.insert(user);

    userAccountMapper.insert(userAccount);

    return true;
  }

}